﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Meqs.Entity;
using Meqs.Logic.Interface;

public partial class School_Teacher : ContainerBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //初始化 repeat控件
    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind(1);
    }

    private void Bind(int pageIndex)
    {
        //获取查询条件类型和值
        int qType = int.Parse(HFQueryType.Value);
        string qValue = HFQueryValue.Value;

        ITeacherInfoLogic teacherInfoLogic = Container<ITeacherInfoLogic>.Instance;
        ISchPremLogic schPremLogic = Container<ISchPremLogic>.Instance;
        List<TeacherInfo> list = null;
        List<SchPrem> spList = null;
        Hashtable ht = new Hashtable();

        switch (qType)
        {
            //0: 查询所有
            case 0:
                //根据条件查询数据库
                ht.Add("SchoolMark", Common.GetConstSchoolMark());
                ht.Add("IsEffect", true);
                ht.Add("IsAdmin", false);
                list = (List<TeacherInfo>)teacherInfoLogic.FindAll(ht);

                //分页处理
                PagerBind(ConversionView(list), pageIndex);

                break;
            //1: 按教工工号查询
            case 1:
                //根据条件查询数据库
                ht.Add("SchoolMark", Common.GetConstSchoolMark());
                ht.Add("IsEffect", true);
                ht.Add("TeachCode", qValue);
                ht.Add("IsAdmin", false);
                list = (List<TeacherInfo>)teacherInfoLogic.FindAll(ht);

                //分页处理
                PagerBind(ConversionView(list), pageIndex);

                break;
            //2: 按教工姓名查询
            case 2:
                //根据条件查询数据库
                ht.Add("SchoolMark", Common.GetConstSchoolMark());
                ht.Add("IsEffect", true);
                ht.Add("TeachName", qValue);
                ht.Add("IsAdmin", false);
                list = (List<TeacherInfo>)teacherInfoLogic.FindAll(ht);

                //分页处理
                PagerBind(ConversionView(list), pageIndex);

                break;
            //3: 按权限查询
            case 3:
                //根据条件查询数据库
                ht.Add("PNo", qValue);
                spList = (List<SchPrem>)schPremLogic.FindAll(ht);
                list = new List<TeacherInfo>();
                foreach (SchPrem schPrem in spList)
                {
                    TeacherInfo teacherInfo = teacherInfoLogic.FindById(schPrem.TeacherID);
                    if (teacherInfo != null)
                    {
                        if ((teacherInfo.SchoolMark == Common.GetConstSchoolMark()) && (teacherInfo.IsEffect == true))
                            list.Add(teacherInfo);
                    }
                }

                //分页处理
                PagerBind(ConversionView(list), pageIndex);

                break;
            default:
                break;
        }
    }

    //分页处理
    private void PagerBind(List<VTeacherInfo> srcList, int pageIndex)
    {
        AspNetPager1.RecordCount = srcList.Count;
        AspNetPager1.CurrentPageIndex = pageIndex;

        int index = (pageIndex - 1) * AspNetPager1.PageSize;
        int count = 0;
        if (srcList.Count - index >= AspNetPager1.PageSize) count = AspNetPager1.PageSize;
        else count = srcList.Count - index;

        List<VTeacherInfo> pageList = srcList.GetRange(index, count);

        Repeater1.DataSource = pageList;
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        Wuqi.Webdiyer.PageChangedEventArgs pc = e as Wuqi.Webdiyer.PageChangedEventArgs;

        Bind(pc.NewPageIndex);
    }

    //转换为教工信息视图显示
    private static List<VTeacherInfo> ConversionView(List<TeacherInfo> srcList)
    {
        List<VTeacherInfo> vTeacherInfoList = new List<VTeacherInfo>();

        foreach (TeacherInfo teacherInfo in srcList)
        {
            string sex = teacherInfo.Sexy == 0 ? "男" : "女";
            string sBirthDay = "";
            string sInSchoolDate = "";
            if (teacherInfo.Birthday.Year != DateTime.MaxValue.Year)
                sBirthDay = teacherInfo.Birthday.ToShortDateString();
            if (teacherInfo.InSchoolDate.Year != DateTime.MaxValue.Year)
                sInSchoolDate = teacherInfo.InSchoolDate.ToShortDateString();

            VTeacherInfo vTeacherInfo = new VTeacherInfo(teacherInfo.RID, teacherInfo.TeachCode, teacherInfo.TeachName, teacherInfo.Passwd,
                                                         teacherInfo.Sexy, teacherInfo.Birthday, teacherInfo.InSchoolDate, teacherInfo.IsEffect,
                                                         teacherInfo.IsAdmin, teacherInfo.SchoolMark, teacherInfo.TeachNamePinYin,
                                                         sex, sBirthDay, sInSchoolDate);
            vTeacherInfoList.Add(vTeacherInfo);
        }

        return vTeacherInfoList;
    }

    /// <summary>
    /// 添加教工信息
    /// </summary>
    /// <param name="teacherInfo"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int InsertTeacherInfo(TeacherInfo teacherInfo, List<int> premlist)
    {
        teacherInfo.IsAdmin  = 0;
        teacherInfo.IsEffect = true;
        teacherInfo.Passwd = Common.GetStartPwd();
        teacherInfo.SchoolMark = Common.GetConstSchoolMark();

        if (teacherInfo.Birthday == DateTime.MinValue)
            teacherInfo.Birthday = DateTime.MaxValue;

        if (teacherInfo.InSchoolDate == DateTime.MinValue)
            teacherInfo.InSchoolDate = DateTime.MaxValue;

        try
        {
            //转化首字母拼音
            teacherInfo.TeachNamePinYin = Sunrise.Spell.Spell.MakeSpellCode(teacherInfo.TeachName, Sunrise.Spell.SpellOptions.EnableUnicodeLetter);
            if (teacherInfo.TeachNamePinYin == "")
            {
                teacherInfo.TeachNamePinYin = " ";
            }
        }
        catch
        {
            teacherInfo.TeachNamePinYin = " ";
        }


        int teacherId = Container<ITeacherInfoLogic>.Instance.Create(teacherInfo);

        //添加教师权限
        List<SchPrem> schPremList = new List<SchPrem>();
        foreach (int premNo in premlist)
        {
            SchPrem schPrem = new SchPrem(teacherId, premNo);
            schPremList.Add(schPrem);
        }
        Container<ISchPremLogic>.Instance.InsertSchPrem(schPremList);

        return teacherId;
    }

    /// <summary>
    /// 修改教工信息
    /// </summary>
    /// <param name="teacherInfo"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int UpdateTeacherInfo(TeacherInfo teacherInfo, List<int> premlist)
    {
        if (teacherInfo.Birthday == DateTime.MinValue)
            teacherInfo.Birthday = DateTime.MaxValue;

        if (teacherInfo.InSchoolDate == DateTime.MinValue)
            teacherInfo.InSchoolDate = DateTime.MaxValue;

        ITeacherInfoLogic teacherInfoLogic = Container<ITeacherInfoLogic>.Instance;

        //根据ID获取要修改的教工信息
        TeacherInfo newTeacherInfo = teacherInfoLogic.FindById(teacherInfo.RID);
        if (newTeacherInfo == null) return 0;
        newTeacherInfo.TeachCode    = teacherInfo.TeachCode;
        newTeacherInfo.TeachName    = teacherInfo.TeachName;
        newTeacherInfo.Sexy         = teacherInfo.Sexy;
        newTeacherInfo.Birthday     = teacherInfo.Birthday;
        newTeacherInfo.InSchoolDate = teacherInfo.InSchoolDate;

        try
        {
            //转化首字母拼音
            newTeacherInfo.TeachNamePinYin = Sunrise.Spell.Spell.MakeSpellCode(newTeacherInfo.TeachName, Sunrise.Spell.SpellOptions.EnableUnicodeLetter);
            if (teacherInfo.TeachNamePinYin == "")
            {
                teacherInfo.TeachNamePinYin = " ";
            }
        }
        catch
        {
            newTeacherInfo.TeachNamePinYin = " ";
        }

        int n = teacherInfoLogic.Update(newTeacherInfo);

        //修改教师权限
        List<SchPrem> schPremList = new List<SchPrem>();
        foreach (int premNo in premlist)
        {
            SchPrem schPrem = new SchPrem(newTeacherInfo.RID, premNo);
            schPremList.Add(schPrem);
        }
        Container<ISchPremLogic>.Instance.UpdateSchPrem(newTeacherInfo.RID, schPremList);

        return n;
    }

    /// <summary>
    /// 删除教工信息
    /// </summary>
    /// <param name="listUserId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int DeleteTeacherInfo(List<int> listUserId)
    {
        int n = Container<ITeacherInfoLogic>.Instance.DeleteTeacherInfo(listUserId);
        
        ////删除教工权限信息
        //foreach (int tid in listUserId)
        //{
        //    Container<ISchPremLogic>.Instance.Delete(tid);
        //}

        //删除教工-班级-课程联系表中的相关记录
        Container<ITeachClsCauseLogic>.Instance.DeleteClsCauseTeacher(listUserId);

        //清空这个教师的所有班主任权限，从班级表中将HeaderID置为0
        Container<IClsInfoLogic>.Instance.UpdateHeader(listUserId);

        foreach (int i in listUserId)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TeacherID", i);
            ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
            ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
            ht.Add("SchoolMark", SchoolMark);
            Container<ITeacherRolesLogic>.Instance.DeleteBy(ht);
        }


        return n;
    }

    //查询教工信息（根据ID）
    [System.Web.Services.WebMethod]
    public static TeacherInfo SelectTeacherInfo(int teacherId)
    {
        ITeacherInfoLogic teacherInfoLogic = Container<ITeacherInfoLogic>.Instance;
        TeacherInfo ti = teacherInfoLogic.FindById(teacherId);
        return ti;
    }

    //获取教师权限信息
    [System.Web.Services.WebMethod]
    public static List<int> GetTeacherPrem(int teacherId)
    {
        List<int> rstList = new List<int>();
        ISchPremLogic schPremLogic = Container<ISchPremLogic>.Instance;

        Hashtable ht = new Hashtable();
        ht.Add("TeacherID", teacherId);
        List<SchPrem> schPremList = (List<SchPrem>)schPremLogic.FindAll(ht);
        foreach (SchPrem schPrem in schPremList)
        {
            //系统编号与权限编号组合
            int premNo = 10 + schPrem.PNo;
            rstList.Add(premNo);
        }

        return rstList;
    }

    //验证教工工号，不能重复
    [System.Web.Services.WebMethod]
    public static int ValidateTeacherInfo(TeacherInfo teacherInfo)
    {
        int result = 0;
        ITeacherInfoLogic teacherInfoLogic = Container<ITeacherInfoLogic>.Instance;

        //1.新增
        if (teacherInfo.RID == 0)
        {
            Hashtable tableCode = new Hashtable();
            tableCode.Add("SchoolMark", Common.GetConstSchoolMark());
            tableCode.Add("TeachCode", teacherInfo.TeachCode);
            tableCode.Add("IsEffect",1);
            result = teacherInfoLogic.FindCount(tableCode);
            //学校编号在有效的数据里面重复
            if (result > 0) return 1;

            
            if (tableCode.ContainsKey("IsEffect"))
                tableCode["IsEffect"] = 0;

            result = teacherInfoLogic.FindCount(tableCode);
            //课程名称重复
            if (result > 0)
                return 2;       //在无效数据里面已经存在这个课程编号，则返回2  
        }
        //2.修改
        else
        {
            TeacherInfo ti = teacherInfoLogic.FindById(teacherInfo.RID);
            //修改了工号
            if (ti.TeachCode.ToUpper() != teacherInfo.TeachCode.ToUpper())
            {
                Hashtable tableCode = new Hashtable();
                tableCode.Add("SchoolMark", Common.GetConstSchoolMark());
                tableCode.Add("TeachCode", teacherInfo.TeachCode);
                tableCode.Add("IsEffect", 1);
                result = teacherInfoLogic.FindCount(tableCode);
                //学校编号重复
                if (result > 0) 
                    return 1;
                
                if (tableCode.ContainsKey("IsEffect"))
                    tableCode["IsEffect"] = 0;

                result = teacherInfoLogic.FindCount(tableCode);
                //课程名称重复
                if (result > 0)
                    return 2;       //在无效数据里面已经存在这个课程编号，则返回2  
            }
        }

        return result;
    }

    //验证教工记录是否能被删除
    [System.Web.Services.WebMethod]
    public static int ValidateDeleteTeacher(List<int> listUserId)
    {
        int result = 0;
        ITeachClsCauseLogic teachClsCauseLogic = Container<ITeachClsCauseLogic>.Instance;
        IClsInfoLogic clsInfoLogic = Container<IClsInfoLogic>.Instance;

        foreach (int id in listUserId)
        {
            Hashtable ht1 = new Hashtable();
            ht1.Add("TeachID", id);
            result = teachClsCauseLogic.FindCount(ht1);
            //教工-班级-课程联系表是否有相关记录
            if (result > 0) return result;

            Hashtable ht2 = new Hashtable();
            ht2.Add("SchoolMark", Common.GetConstSchoolMark());
            ht2.Add("IsEffect", true);
            ht2.Add("HeaderID", id);
            result = clsInfoLogic.FindCount(ht2);
            //班级表是否有相关记录
            if (result > 0) return result;
        }

        return result;
    }

    /// <summary>
    /// 批量设置教师密码
    /// </summary>
    /// <param name="listUserId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int ResetPassword(List<int> listUserId)
    {
        string strStartPwd = Common.GetStartPwd();

        int n = Container<ITeacherInfoLogic>.Instance.ResetPassword(strStartPwd, listUserId);

        return n;
    }

    /// <summary>
    /// 获取子系统列表
    /// </summary>
    /// <param name="listUserId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static List<SystemInfo> GetSystemInfo()
    {
        return (List<SystemInfo>)Container<ISystemInfoLogic>.Instance.FindAll();
    }

    /// <summary>
    /// 获取子系统列表
    /// </summary>
    /// <param name="listUserId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static List<PremInfo> GetPremInfoBySysNo(int sysNo)
    {
        Hashtable ht = new Hashtable();
        ht.Add("SystemNo", sysNo);
        ht.Add("PremType", 2);

        return (List<PremInfo>)Container<IPremInfoLogic>.Instance.FindAll(ht);
    }

    /// <summary>
    /// 获取教师的所有权限列表
    /// </summary>
    /// <param name="listUserId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static List<SchPrem> GetTeacherPremList(int teacherId)
    {
        Hashtable ht = new Hashtable();
        ht.Add("TeacherID", teacherId);

        return (List<SchPrem>)Container<ISchPremLogic>.Instance.FindAll(ht);
    }

    /// <summary>
    /// 批量设置教工权限
    /// </summary>
    /// <param name="listUserId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int BatchSetPrem(List<int> listUserId, List<int> premList)
    {
        Container<ISchPremLogic>.Instance.BatchUpdateSchPrem(listUserId, premList);

        return 1;
    }

    /// <summary>
    /// 判断是否有权限
    /// </summary>
    /// <param name="listUserId"></param>
    /// <returns></returns>
    public static string HaveAnyPrem(object uid)
    {
        string result = "";

        int tid = (int)uid;
        Hashtable ht = new Hashtable();
        ht.Add("TeacherID", tid);
        int n = Container<ISchPremLogic>.Instance.FindCount(ht);

        if (n > 0)
            result = "visible";
        else
            result = "hidden";

        return result;
    }
}
